// @Title businessProcess.go
// @Description:
// @Author 姓名编写
// @Date 2024/3/18
// @Update 姓名编写 2024/3/18

/**************************************************
*Copyright(C).2016-2024,瀚辰光翼⽣物科技有限公司
*文件名：businessProcess.go
*内容简述：*
*文件历史：
author 姓名编写 创建 2024/3/18
**************************************************/
package process

import (
	_interface "cec-server/app/common/interface"
	"cec-server/app/common/process/action"
	"cec-server/app/model"
	"github.com/gin-gonic/gin"
)

var SendProcess = NewBusinessProcess(SendBusinessProcess)

const (
	// BusinessProcess 业务流程
	SendBusinessProcess = iota
	Withdraw
)

type BusinessProcess struct {
	Process []_interface.Process
}

func NewBusinessProcess(code int) *BusinessProcess {
	var bp = BusinessProcess{}
	switch code {
	case SendBusinessProcess:
		bp.AddProcess(action.NewPreCheckoutAction(),
			action.NewAssembleAction(),
			action.NewPostCheckoutAction(),
			action.NewSendMessage())
	case Withdraw:
		bp.AddProcess(action.NewSendMessage(), action.NewPreCheckoutAction(), action.NewAssembleAction(), action.NewPostCheckoutAction())
	default:
		panic("未找到业务流程")
	}
	return &bp
}

func (bp *BusinessProcess) AddProcess(process ..._interface.Process) {
	if len(process) > 0 {
		bp.Process = append(bp.Process, process...)
	}
}

func (bp *BusinessProcess) RunProcess(context *gin.Context, model *model.SendTaskModel) error {
	for _, process := range bp.Process {
		err := process.BusinessProcess(context, model)
		if err != nil {
			return err
		}
	}
	return nil
}
